home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 11355 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.7 KB

  1. Path: news.crystalball.com!news
  2. From: Larry Weiss <lfw@oc.com>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Determining the length of an int in string form
  5. Date: Tue, 19 Mar 1996 19:56:35 -0600
  6. Organization: crystalball.com
  7. Message-ID: <314F65D3.6A8@oc.com>
  8. References: <3146D058.DD7@cbm.com> <4i7uth$qph@inet-nntp-gw-1.us.oracle.com> <DoE38u.GIH@iquest.net> <4in1ga$ogk@airdmhor.gen.nz> <Pine.SOL.3.91.960319172332.4535A-100000@darwin.UCSC.EDU>
  9. NNTP-Posting-Host: external.oc.com
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0 (Win16; I)
  14.  
  15. Hal Royaltey wrote:
  16.  > 
  17.  > On 20 Mar 1996, Simon Hosie wrote:
  18.  > > wkaufman@wkaufman.us.oracle.com (William Kaufman) wrote:
  19.  > > +In article <3146D058.DD7@cbm.com> Dave Payne <paynedc@cbm.com> writes:
  20.  > > +] I have a variable of type int, and I would like to use the sprintf()
  21.  > > +] function to write this variable to a string.  However, I want to
  22.  > > +] dynamically allocate the space for the string, and only malloc enough
  23.  > > +] space to hold the int.
  24.  > 
  25.  > Try taking the base 10 log of the number, add 1, and discard the fractional
  26.  > portion.  That oughta give you the right number of characters, excluding a
  27.  > sign character.   However, I can't imagine any circumstance under which
  28.  > taking base 10 logs is better than simply wasting a few bytes of string 
  29.  > space.
  30.  
  31.  
  32. Maybe you have a really large number of strings to allocate?
  33.  
  34. Anyway, myself, I like precision and safety in knowing that the storage
  35. absolutely will be used properly.   
  36.  
  37. I've seen some suggestions to try to fprintf() once to a file (maybe a 
  38. "bit-bucket") to get a handle on how many characters sprintf() may use,
  39. but I don't think that that's guaranteed by the C Standard to absolutely
  40. correspond.
  41.